﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace 两数相加
{
    class Program
    {
        public ListNode AddTwoNumbers(ListNode L1, ListNode L2)
        {
            a = "";
            var a11 = num(L1);
            a = "";
            var a12 = num(L2);
            var all = a11 + a12;
            Console.WriteLine("all " + a11.ToString ());
            Console.WriteLine("a12 " + a12.ToString());
            string strall = all.ToString();
            Console.WriteLine("strall " + strall);

            ListNode node = listNode(ref list,strall);
            return node;
        }
        string  a;
        double num(ListNode test)
        {
            a += test.val.ToString();
            if (test.next!=null)
            {
                return num(test.next);
            }
            Console.WriteLine("a "+a);
            string b ="";
            for (int i = a.ToString().Length-1; i >= 0; i--)
            {
                b += a.ToString()[i];
            }
            var x = double.Parse(b);
            Console.WriteLine("x "+x);
            if (x.ToString ().ToUpper().Contains("E"))
            {
                double c = double.Parse(b.ToUpper().Split('E')[0].ToString());//整数部分
                //double result = ChangeDataToD(x.ToString());
                decimal result =0;
                ChkNum(x.ToString(), ref result);
               
                Console.WriteLine("result "+ result);
                return Convert.ToDouble(result);
            }
            else
            {
                return x;
            }

        }
        ListNode list = new ListNode(0);
        ListNode listNode(ref ListNode lists, string str)
        {
            for (int i = str.Length - 1; i >= 0; i--)
            {
                Console.WriteLine("lists "+i);
                lists = new ListNode(int.Parse(str[i].ToString()));
                if (i - 1 >= 0)
                {
                    lists.next = new ListNode(int.Parse(str[i - 1].ToString()));
                    return listNode(ref lists.next, str.Substring(0, str.Length - 1));

                }
            }
            return list;
        }
        //科学计数法转普通
        private double ChangeDataToD(string a)
        {
            double result = -1;
            if (a.ToUpper().Contains("E"))
            {
                double b = double.Parse(a.ToUpper().Split('E')[0].ToString());//整数部分
                double c = double.Parse(a.ToUpper().Split('E')[1].ToString());//指数部分
                result = b * Math.Pow(10, c);
            }
            else
            {
                result = double.Parse(a);
            }
            return result;
        }
        private bool ChkNum(string num, ref decimal CompleteNum)
        {
            bool result = false;
            bool resultSymbol = num.Contains("*");
            bool result0 = num.Contains("^");
            if ((resultSymbol == true) && (result0 == true))
            { //当数字中有*和^的时候，进行下面的判断
                int IntSymbol = num.IndexOf("*");
                int Symbol0 = num.IndexOf("^");
                if (((Symbol0 - IntSymbol) == 3))
                {//当*在^前面的时候
                    string numA = num.Substring(0, IntSymbol);//截取*号前面的数字（基数）；
                    string numB = num.Substring(IntSymbol + 1, Symbol0 - IntSymbol - 1);//截取10；
                    string numC = num.Substring(Symbol0 + 1, num.Length - Symbol0 - 1);//获得幂次数
                    Regex regNum0 = new Regex(@"^(\-|\+)?\d+(\.\d+)?$");
                    Regex regNum2 = new Regex(@"^-[1-9]\d*$|^[1-9]\d*$");
                    if ((regNum0.IsMatch(numA)) && (numB == "10") && (regNum2.IsMatch(numC)))
                    {
                        decimal dcNumA;
                        decimal.TryParse(numA, out dcNumA);
                        decimal dcNumC;
                        decimal.TryParse(numC, out dcNumC);//将幂次数转换成decimal类型
                        decimal zhengshu = 10;
                        if (dcNumC > 0)
                        {//当幂次数为整数的时候
                            for (int i = 0; i < dcNumC - 1; i++)
                            {
                                zhengshu *= 10;
                            }
                        }
                        else
                        {//当幂次数为负数的时候
                            for (int i = 0; i < Math.Abs(dcNumC) + 1; i++)
                            {
                                zhengshu /= 10;
                            }
                        }
                        CompleteNum = dcNumA * zhengshu;
                        result = true;
                    }
                    else
                    {
                        result = false;
                    }
                }
                else
                {
                    result = false;
                }
            }
            else
            {
                result = false;
            }
            return result;
        }



        //二
        //public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
        //{
        //    ListNode resultNode = new ListNode(0);
        //    var preNode = resultNode;
        //    int val = 0;
        //    while (l1 != null || l2 != null)
        //    {
        //        val = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + val;

        //        preNode.next = new ListNode(val % 10);
        //        preNode = preNode.next;
        //        val = val / 10;
        //        l1 = l1?.next;
        //        l2 = l2?.next;
        //    }
        //    if (val != 0) preNode.next = new ListNode(val);
        //    return resultNode.next;
        //}
    }
    public class ListNode
    {
        public int val;
        public ListNode next;
        public ListNode(int x) { val = x; }
    }


    
}
